package com.dimbottom.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * Http 请求模板解析器
 */
public class InfoParser {

    private String NBUT_PROPERTIES_LOCATION = "nbut.json";

    /**
     * 加载 NBUT_PROPERTIES_LOCATION 指向的文件中的指定模板，这里只有两类模板，key 为 query、submit，分别用于
     * 查询以往提交信息 和 今日提交信息。
     *
     * @param key 模板的唯一标识符
     * @return {@link com.dimbottom.util.InfoParser.BasicInfo}
     * @throws IOException 文件不存在则抛出错误
     */
    public BasicInfo loadInfo(String key) throws IOException {
        InputStream resource = Connection.class.getClassLoader().getResourceAsStream(NBUT_PROPERTIES_LOCATION);
        assert resource != null;
        String str = IOUtils.toString(resource, StandardCharsets.UTF_8);
        JSONObject properties = JSON.parseObject(str);
        JSONObject data = properties.getJSONObject(key);
        return parse(data);
    }

    /**
     * @param data 模板数据
     * @return {@link com.dimbottom.util.InfoParser.BasicInfo}
     */
    public BasicInfo parse(JSONObject data) {
        String Url = data.getString("URL");
        String Method = data.getString("Method");
        JSONObject tmp = data.getJSONObject("Header");
        List<Header> Headers = new ArrayList<>(tmp.size());
        tmp.forEach((s, o) -> Headers.add(new BasicHeader(s, o.toString())));
        JSONObject Content = data.getJSONObject("Content");
        return new BasicInfo(Url, Method, Headers, Content);
    }

    public String getLocation() {
        return NBUT_PROPERTIES_LOCATION;
    }

    public void setLocation(String location) {
        this.NBUT_PROPERTIES_LOCATION = location;
    }

    /**
     * Http 请求的模板
     */
    public static class BasicInfo {
        private String Url;
        private String Method;
        private List<Header> Headers;
        private JSONObject Content;

        public BasicInfo(String url, String method, List<Header> headers, JSONObject content) {
            Url = url;
            Method = method;
            Headers = headers;
            Content = content;
        }

        public String getUrl() {
            return Url;
        }

        public void setUrl(String url) {
            Url = url;
        }

        public String getMethod() {
            return Method;
        }

        public void setMethod(String method) {
            Method = method;
        }

        public List<Header> getHeaders() {
            return Headers;
        }

        public void setHeaders(List<Header> headers) {
            Headers = headers;
        }

        public String getContent() {
            return Content.toString();
        }

        public void setContent(JSONObject content) {
            this.Content = content;
        }

        public void setQueryStudentCode(String studentCode) {
            JSONObject variables = this.Content.getJSONObject("variables");
            variables.put("studentCode", studentCode);
        }

        public void setSubmitStudentCode(String studentCode) {
            JSONObject input = this.Content.getJSONObject("variables").getJSONObject("input");
            input.put("studentCode", studentCode);
        }

        public void setSubmitVariables(JSONObject variables) {
            JSONObject input = this.Content.getJSONObject("variables").getJSONObject("input");
            input.forEach((s, o) -> input.put(s, variables.getJSONObject("variables").get(s)));
        }

        @Override
        public String toString() {
            return "BasicInfo{" +
                    "Url='" + Url + '\'' +
                    ", Method='" + Method + '\'' +
                    ", Headers=" + Headers +
                    ", Content=" + Content +
                    '}';
        }
    }

}
